package com.net.xpay.stat.scheduler;

import com.net.xpay.common.helper.NoticeHelper;
import com.net.xpay.common.service.UserTransStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 用户月转账数据定时任务
 **/
@Slf4j
@Component
public class UserTransMonthStatScheduler {

    @Autowired
    private UserTransStatService userTransStatService;

    @Autowired
    private NoticeHelper noticeHelper;


    @Scheduled(cron = "0 0 6 1 * *")
    public void doUserTransMonthStatTask() {
        try {
            log.info("start schedule doUserTransMonthStatTask|now={}", LocalDateTime.now());
            int count = 1;//最近1个月
            while (count > 0) {
                long t1 = System.currentTimeMillis();
                LocalDateTime now = LocalDateTime.now().minusMonths(count);
                userTransStatService.doUserSafePayTransMonthStat(now.getYear(), now.getMonthValue());
                long t2 = System.currentTimeMillis();
                log.info("end schedule doUserSafePayTransMonthStat|now={}|cost={}", LocalDateTime.now(), t2 - t1);
                noticeHelper.sendSystemInfoNotice("用户" + now.getMonthValue() + "月转账金额和次数统计完成，耗费时间：" + (t2 - t1), "text");
                count--;
            }

        } catch (Throwable e) {
            log.warn("doUserTransMonthStatTask error", e);
            noticeHelper.sendSystemMonitorNotice("用户月转账金额和次数统计失败|doUserTransMonthStatTask error", "text");
        }
    }

}